﻿using Lucid.ESA.ClientDataSystem.Domain.Common;
using Lucid.ESA.ClientDataSystem.Domain.Entities;
using Lucid.ESA.ClientDataSystem.Domain.ParameterClasses;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Lucid.ESA.ClientDataSystem.UnitTests.Domain
{
    [TestClass]
    public class SiteTests
    {
        [TestMethod]
        public void CanCreateValidSite()
        {
            Client client = Client.Create("Mauve Corporation");

            CreateSiteParameters parameters = new CreateSiteParameters()
            {
                AddressLine1 = "6 Orange Street",
                AddressLine2 = "Greenville",
                AddressLine3 = "Whiteshire",
                MainSite = true,
                Client = client
            };

            Site site = Site.Create(parameters);
            ValidationMessageCollection messages = site.Validate();

            Assert.AreEqual(0, messages.Count);
            Assert.AreEqual("6 Orange Street", site.AddressLine1);
            Assert.AreEqual("Greenville", site.AddressLine2);
            Assert.AreEqual("Whiteshire", site.AddressLine3);
            Assert.IsTrue(site.MainSite);
            Assert.AreEqual("Mauve Corporation", site.Client.Name);
            Assert.AreEqual(1, site.Client.Sites.Count);
        }

        [TestMethod]
        public void CanEditValidSite()
        {
            Client client = Client.Create("Mauve Corporation");

            CreateSiteParameters createParameters = new CreateSiteParameters()
            {
                AddressLine1 = "6 Orange Street",
                AddressLine2 = "Greenville",
                AddressLine3 = "Whiteshire",
                MainSite = true,
                Client = client
            };

            Site site = Site.Create(createParameters);

            EditSiteParameters editParameters = new EditSiteParameters()
            {
                AddressLine1 = "7 Orange Street",
                AddressLine2 = "Greyville",
                AddressLine3 = "Blackshire",
                MainSite = false,
            };

            site.Edit(editParameters);
            ValidationMessageCollection messages = site.Validate();

            Assert.AreEqual(0, messages.Count);
            Assert.AreEqual("7 Orange Street", site.AddressLine1);
            Assert.AreEqual("Greyville", site.AddressLine2);
            Assert.AreEqual("Blackshire", site.AddressLine3);
            Assert.IsFalse(site.MainSite);
            Assert.AreEqual("Mauve Corporation", site.Client.Name);
            Assert.AreEqual(1, site.Client.Sites.Count);
        }

        [TestMethod]
        public void SiteWithoutClientOrAddressReturnsErrors()
        {
            CreateSiteParameters parameters = new CreateSiteParameters();
            Site site = Site.Create(parameters);
            ValidationMessageCollection messages = site.Validate();

            Assert.AreEqual(2, messages.Count);
            Assert.IsTrue(messages.ContainsError("Client", "Client is not set."));
            Assert.IsTrue(messages.ContainsError("AddressLine1", "Address line 1 cannot be empty."));
        }

        [TestMethod]
        public void SiteReturnsErrorWhenAddingTwoMainSites()
        {
            Client client = Client.Create("Mauve Corporation");

            CreateSiteParameters parameters = new CreateSiteParameters()
            {
                AddressLine1 = "6 Orange Street",
                AddressLine2 = "Greenville",
                AddressLine3 = "Whiteshire",
                MainSite = true,
                Client = client
            };

            Site site1 = Site.Create(parameters);

            parameters = new CreateSiteParameters()
            {
                AddressLine1 = "4 Olive Road",
                AddressLine2 = "Redtown",
                AddressLine3 = "Purpleshire",
                MainSite = true,
                Client = client
            };

            Site site2 = Site.Create(parameters);

            ValidationMessageCollection messages = site2.Validate();

            Assert.AreEqual(1, messages.Count);
            Assert.IsTrue(messages.ContainsError("MainSite", "Site cannot be set as main site because a main site already exists for this client."));
        }
    }
}
